<?php
/**
 * 根据订单重置用户的PV
 */
class RepvAction extends BaseAction {
	
	/**
	 * 根据订单统计用户的PV
	 */
	function index() {
		// 使用此程序前，请先清空元用户的积分 、优惠券、订单的赠送记录。
		// 上一次使用时间 2012-12-07 16:50 1354870729
		exit ();
		/**
		 * 查询订单列表
		 */
		$order = M ( "Order" );
		$orderdata = M ( "OrderData" );
		$orderother = M ( "OrderOther" );
		$user = M ( "User" );
		$where = array ();
		$where ['status'] = array (
				"neq",
				5 
		);
		$olist = $order->field ( "id,sn,userid,status,pay_status,shipping_status,add_time,jiazs,jiayufu" )->where ( $where )->select ();
		foreach ( $olist as $v ) {
			$user->where ( "id=" . $v ['userid'] )->setInc ( "point", $v ['jiayufu'] );
			$this->_wuserassets ( 0, 0, $v ['userid'], '', 'PV_CHANG', $v ['sn'], $v ['jiayufu'], 0 );
			$this->_uporderpv ( $v ['id'], $v ['userid'], $v ['jiayufu'], $orderother );
			echo "更新订单：" . $v ['sn'] . '<br>';
		}
	}
	
	/**
	 * 排量给用户赠送BV
	 */
	function allbv() {
		// 活动用的，批量赠送BV
		// 上一次使用时间 2012-12-15 18:08
		// 上一次使用时间 2012-12-20 16:37 赠送88 8人 ；388 10人；588 1人；
		// 上一次使用时间 2012-12-29 15:39 增加BV1200 11人，500 20人 ，200 3人 17-23充值情况-已经审核.xls
		exit ();
		$userid = $_REQUEST ['userid'];
		$addbv = intval ( $_REQUEST ['addbv'] );
		$userid = explode ( '|', $userid );
		$qishu = $_REQUEST ['qishu'];
		$user = M ( "User" );
		$givedao = M ( "Give" );
		$givelogs = M ( "GiveLogs" );
		foreach ( $userid as $v ) {
			$uid = '';
			$uid = intval ( $v );
			if ($uid) {
				$where = array ();
				$where ['id'] = array (
						"EQ",
						$uid 
				);
				$userinfo = $user->where ( $where )->find ();
				$glog = $gvo = array ();
				$glog ['oldoedu'] = $userinfo ['give'];
				$gvo ['eachoedu'] = $gvo ['firstoedu'] = $addbv / $qishu;
				$uvo ['give'] = $userinfo ['give'] + $gvo ['firstoedu'];
				$uvo ['updatetime'] = time ();
				$oedu = '';
				$user->where ( $where )->setInc ( 'give', $gvo ['firstoedu'] );
				
				// 记录赠送帐户分期
				// 允许一个帐户拥有多个分期的每个分期的唯一码
				$only = short ( $uvo ['updatetime'] . '_' . $uid . '_' . $addbv );
				$glog ['only'] = $gvo ['only'] = $only;
				$gvo ['startday'] = $glog ['createtime'] = $gvo ['createtime'] = $uvo ['updatetime'];
				$gvo ['auth'] = date ( "Ymd" );
				$gvo ['qishu'] = $qishu;
				$gvo ['notoverqishu'] = $qishu - 1;
				$endmon = date ( 'n' ) + $gvo ['notoverqishu'];
				$gvo ['endday'] = mktime ( 0, 0, 0, $endmon, date ( 'd' ) + 1, date ( 'Y' ) );
				$gvo ['status'] = $gvo ['notoverqishu'] > 1 ? '1' : '0';
				$gvo ['isover'] = $gvo ['notoverqishu'] > 1 ? '0' : '1';
				;
				$glog ['userid'] = $gvo ['userid'] = $uid;
				$gvo ['countoedu'] = $addbv;
				$glog ['addoedu'] = $gvo ['overoedu'] = $gvo ['firstoedu'];
				
				$gvo ['overqishu'] = 1;
				$gvo ['updatetime'] = $gvo ['createtime'];
				$gvo ['notoveroedu'] = $gvo ['countoedu'] - $gvo ['overoedu'];
				
				// 写入分期综合信息
				// 分期的总额度为0时不写入分期表
				if ($gvo ['countoedu']) {
					$givedao->add ( $gvo );
				}
				$glog ['mon'] = date ( 'm', $gvo ['createtime'] );
				$glog ['nowoedu'] = $uvo ['give'];
				// 写入到账日志
				// 分期的总额度为0时不写入分期到账日志表
				$givelogs->add ( $glog );
				$urs = $user->where ( $where )->save ( $uvo );
				if ($urs) {
					/*
					 * 还需要写入事件日志 logs 表
					 */
					// ///充值成功短信提示
					$userinfo = $user->where ( "id={$uid}" )->find ();
					$str = '您的账户在' . date ( 'm月d日H时i分' ) . '获赠：' . $addbv . 'BV，已到账' . $gvo ['firstoedu'] . '，BV总额：' . $uvo ['give'];
					// $str.=/*.'现在账户'.$userinfo['amount'].'CV '.$userinfo['give'].'BV*/ '[云采购]';
					echo $uid . ' 赠送' . $addbv . 'BV,BV总额：' . $uvo ['give'] . '<br>';
					$msg = iconv ( "UTF-8", "GB2312//TRANSLIT", $str );
					$dxinf = sendMobiMsg ( $userinfo ['mobile'], $msg );
					
					// echo $dxinf." mmm=".strlen($msg)."<br>";
					// dump($msg);
					// exit();
					$this->_wuserassets ( 0, $gvo ['firstoedu'], $uid );
				/**
				 * 发送短信
				 */
					// $userinfo=$user->where("id={$userid}")->find();
					// $str=$userinfo['username'].' 充值'.$oedu['amount'].'CV '.$oedu['give'].' BV'.'现在账户'.$userinfo['amount'].'CV '.$userinfo['give'].'BV [云采购]';
				} else {
					/*
					 * 还需要写入事件日志 logs 表
					 */
					$this->_ajm ( 300, $uid . ' 操作失败，请重试!<br>', 0 );
				}
			}
		}
		exit ();
	}
	
	/**
	 * 批量派送优惠券
	 * 
	 * @param
	 *        	userid 用户id，多个用户ID使用 | 分割
	 * @param
	 *        	addcoupon 赠送优惠券的面值
	 * @param
	 *        	starttime 赠送优惠券的使用开始时间
	 * @param
	 *        	endtime 赠送优惠券的使用结束时间
	 */
function addcoupon(){
		//活动用的，批量赠送BV
		//派送的优惠券不减少PV
		$userid=$_REQUEST['userid'];
		$addvalue=floatval($_REQUEST['addcoupon']);
		$starttime=$_REQUEST['starttime'];
		$endtime=$_REQUEST['endtime'];
		$remarks=$_REQUEST['remarks'];
		$qishu=intval($_REQUEST['qishu']);
		$zhouqi=$_REQUEST['zhouqi'];
		
		if(empty($addvalue)){
			$this->_ajm(300,'优惠券面值为空，操作失败，请重试!',0);
		}else{
			$addvalue = $addvalue*100; //换算成分
		}
		
		if(empty($userid)){
			$this->_ajm(300,'用户ID读取失败，操作失败，请重试!',0);
		}else{
			$userid=explode('|', $userid);
		}
		$dao=M("UserCoupon");
		$user=M("User");
		$data=array();
		$data['type']=1;
		$data['value']=$addvalue;
		$data['point']=0;
		$data['status']=0;
		$data['cip']=get_client_ip(1);
		$data['createtime']=time();
		$err=0;
		$for=1;
		if($qishu>1){
			$for=$qishu;
		}
		for($i=0;$i<$for;$i++){
			if($i==0){
				$data['starttime']=gettime($starttime);
				$data['endtime']=gettime($endtime);
				$data['remarks']=$remarks;
			}else{
				$addtime=$zhouqi*24*60*60*$i;
				$data['starttime']=$data['starttime']+$addtime;
				$data['endtime']=$data['endtime']+$addtime;
				$data['remarks']=$remarks.'第'.($i+1).'期';
			}
			$str=date("Y-m-d",$data['starttime']).'到'.date("Y-m-d",$data['endtime']).'内有效。';
			$data['remarks'].=$str;
			foreach ($userid as $uid) {
				if(intval($uid)){
					$data['userid']=intval($uid);
					$userinfo=$user->where("id={$data['userid']}")->find();
					if(empty($userinfo)){
						$err++;
						continue;
					}
					$res=$dao->add($data);
					if($res){
						$str='您的账户在'.date('m月d日H时i分').'获赠：价值'.$addvalue.'CV的优惠券。';
						$str.=$uid.$str.'--'.$data['remarks'].'<br>';
						//$str=iconv ( "UTF-8", "GB2312//TRANSLIT",$str );
						//$dxinf=sendMobiMsg($userinfo['mobile'],$str);
						//$this->_ajm(200,$str,1);
					}else{
						$err++;
					}
				}
			}
		}
		if($err>0){
			$this->_ajm(300,'共有'.$err.'条信息出现错误。',0);
		} else {
			$this->_ajm(200,'优惠券生成成功！',0);
		}
	}
	/**
	 * 更新订单的PV信息
	 */
	protected function _uporderpv($oid, $userid, $bakpv, $dao) {
		$info = $dao->where ( "order_id=" . $oid )->find ();
		$data = array ();
		$data ['bakpv'] = $bakpv;
		$data ['isbakpv'] = 1;
		$data ['bakpvtime'] = time ();
		if (empty ( $info )) {
			$data ['order_id'] = $oid;
			$data ['userid'] = $userid;
			$dao->add ( $data );
		} else {
			$dao->where ( "id=" . $info ['id'] )->save ( $data );
		}
	}
	
	/**
	 * 用户资金充值明细
	 * 
	 * @param 预付款变动 $yufu        	
	 * @param 赠送帐户变动 $yufuzs        	
	 * @param 关联用户ID $userid        	
	 * @param 操作授权码 $money_auth        	
	 * @return boolean
	 */
	protected function _wuserassets($yufu = 0, $yufuzs = 0, $userid = 0, $money_auth = '', $changtype = 'ADMIN_ADD', $ordersn = '0', $pv = 0, $mv = 0) {
		if (empty ( $userid )) {
			return false;
		} else {
			$dao = M ( "UserAssets" );
			$user = M ( "User" );
			$userinfo = $user->field ( 'id,amount,give,point,mv' )->where ( "id={$userid}" )->find ();
			if ($userinfo) {
				$data = array ();
				$data ['money_auth'] = $money_auth;
				$data ['changtype'] = $changtype;
				$data ['changmoney'] = $yufu;
				$data ['changmv'] = $mv;
				$data ['changzs'] = $yufuzs;
				$data ['money'] = $userinfo ['amount'];
				$data ['zsmoney'] = $userinfo ['give'];
				$data ['ctime'] = time ();
				$data ['cip'] = ip2long ( get_client_ip () );
				$data ['changjf'] = $pv;
				$data ['changyh'] = 0;
				$data ['yhsn'] = 0;
				$data ['ordersn'] = $ordersn;
				$data ['mv'] = $userinfo ['mv'];
				$data ['jifeng'] = $userinfo ['point'];
				$data ['userid'] = $userinfo ['id'];
				$uaid = $dao->add ( $data );
				if ($uaid) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
	}
	
	/**
	 * 获取订单的详情，并获取有货的产品CV和可用BV
	 */
	function _getdata($orderid, $dao) {
		$info = array ();
		$where = array ();
		$where ['is_ok'] = array (
				'eq',
				1 
		);
		$where ['order_id'] = array (
				'eq',
				$orderid 
		);
		$info = $dao->field ( "sum(price) as cv,sum(jiazhengsong) as bv" )->where ( $where )->select ();
		return $info [0];
	}
	
	/**
	 * 清除表单的bakPV信息
	 */
	function bakpv() {
		// 清楚未返还mv的记录
		$sql = "DELETE FROM iic_order_other WHERE isbakmv=0;";
		
		// 对以返还mv的记录重置返还PV的记录为0
		$sql = "UPDATE iic_order_other SET bakpv=0,isbakpv=0,bakmvtime=0;";
	}
	
	/**
	 * 将用户的PV归0
	 */
	function zorepv() {
		$sql = "UPDATE iic_user SET point=0;";
	}
	
	/**
	 * 删除和PV相关的流水
	 */
	function delasspv() {
		$userassets = M ( "UserAssets" );
		// $sql="DELETE FROM iic_user_assets WHERE changjf<>'0.00'";
		// ALTER TABLE `iic_user_assets` CHANGE `changtype` `changtype` CHAR( 20 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '0' COMMENT '账户变动类别';
		$sql = "UPDATE iic_user_assets SET jifeng='0.00' WHERE jifeng<>'0.00';";
		$sql = "UPDATE iic_user_assets SET changtype=CONCAT(`changtype`,'_BAK') WHERE changjf<>'0.00';";
	}
	
	/**
	 * 清除优惠券
	 */
	function delcopou() {
		$sql = "DELETE FROM iic_user_coupon WHERE 1";
	}
	
	/**
	 * MV清0
	 */
	function clearmv() {
		$sql = "UPDATE iic_user SET mv=0;";
		$sql = "UPDATE iic_user_assets SET changmv='0.00',mv='0.00';";
	}
}
